



#include "CSLEPFilesWidget.h"


CSLEPFilesWorkspaceWidget::CSLEPFilesWorkspaceWidget(QWidget *parent){
	CSLEPFilesWorkspaceWidget(tr(""),parent);
}

CSLEPFilesWorkspaceWidget::CSLEPFilesWorkspaceWidget(const QString& title, QWidget *parent): QDockWidget(title,parent){

	QGroupBox *box = new QGroupBox, *box2, *box3;
	QVBoxLayout *layout = new QVBoxLayout;
	QHBoxLayout *buttonLayout = new QHBoxLayout, *workingDirLayout = new QHBoxLayout;
	QLabel *workingDirLabel;

	tableModelWorkspaceFiles = new ObjectListModel(this);
	tableViewWorkspaceFiles = new QTableView(this);
	tableViewWorkspaceFiles->setModel(tableModelWorkspaceFiles);
	tableViewWorkspaceFiles->setAlternatingRowColors(true);
	tableViewWorkspaceFiles->horizontalHeader()->setStretchLastSection(true);

	lineEditDirectory = new QLineEdit;
	lineEditDirectory->setText(QDir::currentPath());

	buttonAdd = new QPushButton;
	buttonAdd->setText(tr("Add"));
	buttonRemove = new QPushButton;
	buttonRemove->setText(tr("Remove"));

	layout->addWidget(tableViewWorkspaceFiles, Qt::AlignCenter);

	workingDirLabel = new QLabel(tr("Working Directory:"));
	workingDirLabel->setBuddy(lineEditDirectory);
	box2 = new QGroupBox;
	workingDirLayout->addWidget(workingDirLabel);
	workingDirLayout->addWidget(lineEditDirectory);
	box2->setLayout(workingDirLayout);

	box3 = new QGroupBox;
	buttonLayout->addWidget(buttonAdd);
	buttonLayout->addWidget(buttonRemove);
	box3->setLayout(buttonLayout);


	layout->addWidget(box2);
	layout->addWidget(box3);

	box->setLayout(layout);
	setWidget(box);

	connect(buttonAdd, SIGNAL(clicked()), this, SLOT(addCSLEPObj()));
	connect(buttonRemove, SIGNAL(clicked()),this, SLOT(removeCSLEPObj()));	
	connect(this, SIGNAL(workingDirChanged(QString)),this,SLOT(setWorkingDir(QString)));
}

void CSLEPFilesWorkspaceWidget::addCSLEPObj(){

	QString fileName = QFileDialog::getOpenFileName(this,
                                tr("QFileDialog::getOpenFileName()"),
                                lineEditDirectory->text());
	emit workingDirChanged(fileName);
	//SlepFileMethods::writeDMatrix(fileName.append(tr(".2")),SlepFileMethods::readDMatrix(fileName));
	slep_matrix* matrix = SlepFileMethods::readDMatrix(fileName);

	tableModelWorkspaceFiles->insertRows(0,1,QModelIndex());
	QModelIndex index = tableModelWorkspaceFiles->index(0,0,QModelIndex());

	tableModelWorkspaceFiles->setData(index,QFileInfo(fileName).baseName(),Qt::EditRole);
	index = tableModelWorkspaceFiles->index(0,1,QModelIndex());
	char string[100];
	sprintf(string,"%dx%d",matrix->m,matrix->n);

	tableModelWorkspaceFiles->setData(index,QString("").append(string),Qt::EditRole);
	index = tableModelWorkspaceFiles->index(0,2,QModelIndex());
	tableModelWorkspaceFiles->setData(index,QDir(fileName).absoluteFilePath(QFile(fileName).fileName()),Qt::EditRole);

}

void CSLEPFilesWorkspaceWidget::removeCSLEPObj(){
	//tableViewWorkspaceFiles->
}


void CSLEPFilesWorkspaceWidget::setWorkingDir(QString directory){
	QDir *dir = new QDir(directory);
	if( ! dir->isReadable() )
		dir->cdUp();
	lineEditDirectory->setText(dir->absolutePath());
}

slep_matrix* SlepFileMethods::readDMatrix(QString filename){
	slep_matrix* mtrx;
	QFile matrixfile(filename);
	int m=0,n=0,i,j;
	double d;
	
	if (!matrixfile.open(QIODevice::ReadOnly | QIODevice::Text))
         return 0;
    QTextStream in(&matrixfile);
    QString line = in.readLine();
	QTextStream stream(&line);
	while(! stream.atEnd())
		(stream>>d),++n;
	++m;
    while (!in.atEnd())
		in.readLine(), ++m;

	matrixfile.close();
	if (!matrixfile.open(QIODevice::ReadOnly | QIODevice::Text))
         return 0;

	mtrx = slepAllocDenseMatrix(m,n);
	QTextStream in2(&matrixfile);
    for(i=0; i<m;++i){
        QString line = in2.readLine();
		QTextStream stream2(&line);
		for(j=0;j<n-1;j++)
			stream2 >> (mtrx->data.mtrx[i+j*m]) ;
		stream2 >> (mtrx->data.mtrx[i+j*m]);
    }
	matrixfile.close();
	return mtrx;
}

void SlepFileMethods::writeDMatrix(QString filename, slep_matrix* A){
	int i,j;
	QFile matrixfile(filename);
	if (!matrixfile.open(QIODevice::WriteOnly | QIODevice::Text))
         return;
	QTextStream out(&matrixfile);
    for(i=0; i< (A->m);++i){
		for(j=0;j<(A->n-1);j++)
			out << (A->data.mtrx[i+j* (A->m)])<<" ";
		out << (A->data.mtrx[i+j*(A->m)])<<"\n";
    }
	matrixfile.close();

}